﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;

namespace DDD从零起步.Helper
{
    public static class UIHelper
    {
        /// <summary>
        /// 建立一个站点地图
        /// </summary>
        /// <param name="?"></param>
        /// <param name="activeCss"></param>
        /// <returns></returns>
        public static MvcHtmlString CreateSiteMapTree(
            this HtmlHelper html,
            object id,
            string activeCss)
        {
            if (id == null)
                return null;
            int _id = Convert.ToInt32(id);
            SiteMapTree.Init();
            Tree tree = new Tree(SiteMapTree.Data);
            if (tree == null)
                return null;
            var entity = tree.GetAllFather(_id);
            entity.IsCurrent = true;
            StringBuilder result = new StringBuilder();


            FillFatherTree(result, entity, activeCss);
            result.Insert(0, "<div class='lx_indexcen_subnav'>");
            result.Append("</div>");
            return MvcHtmlString.Create(result.ToString());
        }

        static void FillFatherTree(StringBuilder result, SiteMapTree tree, string activeCss)
        {
            if (tree != null)
            {
                result.Insert(0, string.Format("<a href='{0}' title='{1}' class='{2}'>{1}</a>"
                     , tree.LinkUrl
                     , tree.Name
                     , tree.IsCurrent ? activeCss : string.Empty));
                FillFatherTree(result, tree.Father, activeCss);
            }
        }
    }



    /// <summary>
    /// 站点地图
    /// 树型结构
    /// </summary>
    public class SiteMapTree
    {
        public SiteMapTree()
        { }

        public SiteMapTree(int id, string name, string linkUrl, bool isCurrent, int? parentID)
        {
            this.ID = id;
            this.Name = name;
            this.LinkUrl = linkUrl;
            this.IsCurrent = isCurrent;
            this.ParentID = parentID;
        }
        public int ID { get; set; }
        public string Name { get; set; }
        public string LinkUrl { get; set; }
        public bool IsCurrent { get; set; }
        public int? ParentID { get; set; }
        public List<SiteMapTree> Sons { get; set; }
        public SiteMapTree Father { get; set; }

        public static List<SiteMapTree> Data = new List<SiteMapTree>();
        /// <summary>
        /// 初始化，填充基础数据
        /// </summary>
        public static void Init()
        {
            Data.Add(new SiteMapTree(1, "用户中心", "#", false, null));
            Data.Add(new SiteMapTree(2, "我的学习教程", "#", false, 1));
            Data.Add(new SiteMapTree(3, "已购买的课程", "#", false, 2));
            Data.Add(new SiteMapTree(4, "已到期的课程", "#", false, 2));
            Data.Add(new SiteMapTree(5, "已观看的课程", "#", false, 2));
        }
    }

    /// <summary>
    /// 树的操作
    /// </summary>
    public class Tree
    {
        List<SiteMapTree> Data;
        public Tree(List<SiteMapTree> data)
        {
            this.Data = data;
        }

        #region 树的装载
        public SiteMapTree GetCompleteTree()
        {
            var root = Data.First(i => !i.ParentID.HasValue);
            GetSubTree(root);
            return root;
        }

        void GetSubTree(SiteMapTree tree)
        {
            List<SiteMapTree> sons = Data.Where(i => i.ParentID == tree.ID).ToList();
            tree.Sons = sons;
            sons.ForEach(i =>
            {
                i.Father = tree;
                GetSubTree(i);
            });
        }
        #endregion

        #region 得到不完整的树
        public SiteMapTree GetTree(int id)
        {
            var root = Data.First(i => i.ID == id);
            GetSubTree(root);
            return root;
        }
        #endregion

        #region 通过叶子节点得到它所有祖辈节点
        public SiteMapTree GetAllFather(int id)
        {
            var root = Data.First(i => i.ID == id);
            GetFatherTree(root);
            return root;
        }

        void GetFatherTree(SiteMapTree tree)
        {
            SiteMapTree father = Data.FirstOrDefault(i => i.ID == tree.ParentID);
            if (father != null)
            {
                tree.Father = father;
                GetFatherTree(father);
            }
        }
        #endregion
    }
}